package edu.flying.panda.taskmanager;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * Sign Up screen
 * @author FlyingPanda
 */

public class SignUpScreen extends Activity {

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sign_up);

		final Button createUserButton = (Button) findViewById(R.id.sign_up_signUp_button);
		final Button backButton = (Button) findViewById(R.id.sign_up_back_button);

		
		/**
		 * Listener for createUser button, handles possible errors
		 */
		createUserButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {

				String user = ((EditText) findViewById(R.id.sign_up_username))
						.getText().toString();
				String password = ((EditText) findViewById(R.id.sign_up_pass))
						.getText().toString();
				String name = ((EditText) findViewById(R.id.sign_up_name))
						.getText().toString();
				String email = ((EditText) findViewById(R.id.sign_up_email))
						.getText().toString();
				TextView errorText = (TextView) findViewById(R.id.signUp_error);
				 Log.d("sus","1");
				if (TaskManagerActivity.DB.getUser(user) == null && !user.equals("")) {
					 Log.d("sus","2");
					if (password.length() >= 6) {
						if (email.contains("@") && email.contains(".")) {
							if (!name.equals("")) {
								 Log.d("sus","3");
								TaskManagerActivity.DB.addUser(new User( user, password, name, email));
								 Log.d("sus","4");

								Intent myIntent = new Intent(v.getContext(),
										LogInScreen.class);
								startActivity(myIntent);

							} else {
								errorText.setText("Must enter a Name");
							}
						} else {
							errorText.setText("Invalid email");
						}
					} else {
						errorText.setText("Password must be 6 characters long");
					}
				} else {
					errorText.setText("Username taken");
				}

			}
		});

		
		/**
		 * Listener for backButton to cancel signup/return to main screen
		 */
		backButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				Intent myIntent = new Intent(view.getContext(), LogInScreen.class);
				startActivityForResult(myIntent, 0);
			}

		});

	}
}
